package com.freework.util.excel.read.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.freework.util.excel.read.service.rowToObject.RowToObjectI;
import com.freework.util.excel.read.service.rowToObject.SetValueI;
import com.freework.util.excel.read.vo.ColVO;
import com.freework.util.excel.read.vo.SheetVO;

public class ReadSheelDataPOI implements ReadSheelDataI {
	
	public void setCellValue(HSSFSheet aSheet,HSSFCell aCell,Object obj,ColVO col,SetValueI setvalue){
		 int cellType = aCell.getCellType();
         switch (cellType) {
         case 0://Numeric
        	 setvalue.setValue(obj, aCell.getNumericCellValue(), col);
          return ;
         case 1://String
        	 setvalue.setValue(obj,  aCell.getStringCellValue(), col);
         return ;
         case 2://formula
          aSheet.setDisplayFormulas(true);
          setvalue.setValue(obj,  aCell.getNumericCellValue(), col);
          return ;
         case 3://blank
             setvalue.setValue(obj,  aCell.getStringCellValue(), col);

         return ;
         default:
         String error = "error!";
         return ;
         }
	}
	
	public List getData(HSSFSheet	sheet,SheetVO vo, RowToObjectI rowToOject) {
		
			int startrow=vo.getStartrow();
			int endrow=vo.getEndrow()==-1?sheet.getLastRowNum():vo.getEndrow();
			List<ColVO> cols=vo.getCols();
			SetValueI setValue[]=rowToOject.getSetValues();
			List list=new ArrayList((endrow-startrow)+1);
			for (int i = startrow; i <= endrow; i++) {
				HSSFRow row=sheet.getRow(i);
				int j=0;
				Object obj=rowToOject.createObject();
				for (ColVO col : cols) {
					HSSFCell cell=row.getCell(col.getColnum());
					this.setCellValue(sheet, cell, obj, col, setValue[j]);
					j++;
				}
				list.add(obj);
			}
			
			return list;
			
		
		
	}

	public List getData(SheetVO vo, RowToObjectI rowToOject, InputStream in) {
		HSSFWorkbook workbook =null;
	    
			 try {
				workbook = new HSSFWorkbook(in);
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		HSSFSheet	sheet=workbook.getSheetAt(vo.getSheetnum());
		return this.getData(sheet, vo, rowToOject);
	}

	public List<List> getData(List<SheetVO> vos, List<RowToObjectI> rowToOjects,
			InputStream in) {
		HSSFWorkbook workbook =null;
		 try {
			workbook = new HSSFWorkbook(in);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		List lists[]=new List[vos.size()];
		int i=0;
		for (SheetVO sheetVO : vos) {
			RowToObjectI rowToOject=rowToOjects.get(i);
			HSSFSheet	sheet=workbook.getSheetAt(sheetVO.getSheetnum());
			lists[i]=this.getData(sheet, sheetVO, rowToOject);
			i++;
		}
		
		return Arrays.asList(lists);
	}

}
